﻿using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CutShuFa.AutoAction
{
    public class ActionCutBoard : AAutoAction
    {
        public override string ActionName
        {
            get
            {
                return "去边框";
            }
        }

        public override List<Parameter> GetParameters()
        {
            if (parameters == null)
            {
                parameters = new List<Parameter>();
                parameters.Add(new Parameter() { Name = "input", Value = "src_mat", Type = ParameterType.Mat });
                parameters.Add(new Parameter() { Name = "output", Value = "tmp_mat", Type = ParameterType.Mat });
                parameters.Add(new Parameter() { Name = "top", Value = "10", Type = ParameterType.Int });
                parameters.Add(new Parameter() { Name = "left", Value = "10", Type = ParameterType.Int });
                parameters.Add(new Parameter() { Name = "right", Value = "10", Type = ParameterType.Int });
                parameters.Add(new Parameter() { Name = "bottom", Value = "10", Type = ParameterType.Int });
            }
            return parameters;
        }

        public override void Run(AutoActionData data)
        {
            if (this.parameters == null)
            {
                GetParameters();
            }
            string input = parameters.Where(p => p.Name == "input").FirstOrDefault().Value;
            string output = parameters.Where(p => p.Name == "output").FirstOrDefault().Value;

            int top = int.Parse(parameters.Where(p => p.Name == "top").FirstOrDefault().Value);
            int left = int.Parse(parameters.Where(p => p.Name == "left").FirstOrDefault().Value);
            int right = int.Parse(parameters.Where(p => p.Name == "right").FirstOrDefault().Value);
            int bottom = int.Parse(parameters.Where(p => p.Name == "bottom").FirstOrDefault().Value);

            int width = data.matDic[input].Width - left - right;
            int height = data.matDic[output].Height - top - bottom;

            // 截取区域
            Rect rect = new Rect(left, top, width, height);
            data.matDic[output] = new Mat(data.matDic[input], rect);
        }

    }
}
